/*
 * game.c
 *
 * This will handle the rules of the game and might handle the display
 *
 *  Created on: Dec 8, 2011
 *      Author: Jonathan Marokhovsky
 */

#import <stdio.h>
#import <stdlib.h>
#import "game.h"
#import "display.h"

#define DEBUG	1

Player newPlayer(char av, char *name, char *ip, int port);
Bullet shotFired(int player, Direction dir, Point playerPos);

int main()
{
	int maxPlayers = MAX_PLAYERS;
	int maxBullets = MAX_BULLETS;
	int mapWidth = MAP_WIDTH;
	int playerCount, bulletCount, i;

	/* Arrays of things */
	Player players[maxPlayers]; // a list of all "connected" users
	Bullet bullets[maxBullets];

	// The map being used is theMap
	if (DEBUG)
	{
		printf("Initial Map: \n");
		for (i = 0; i < MAP_HEIGHT; i++)
			printf("%s", theMap);
	}
	/* Try making a user and a bullet */
	if (DEBUG) printf("Trying to make a new player.\n");
	newPlayer('A', "Bob", "192.168.1.1", 5000);
	/* Try to make the new screen using ncurses */
	if (DEBUG) printf("Trying to make a new window.\n");
	theWindow = newWin();
	displayWin(theWindow, theMap);
	getMoves(theWindow);

	//sleep(1);
	cleanUp(0);
	printf("The character sent was: '%s'", theMessage);

	/* Input information */
	/*
	 * Movement:
	 * 		W - Move North
	 * 		A - Move West
	 * 		S - Move South
	 * 		D - Move East
	 */
	/*
	 * Shooting:
	 * 		Up arrow - Shoot North
	 * 		Left arrow - Shoot West
	 * 		Down arrow - Shoot South
	 * 		Right arrow - Shoot East
	 */
	/* Rules to enforce: */
	/* When a bullet hits a wall, get rid of the bullet */
	/* When a bullet hits an person*/
		/* The bullet disappears */
		/* The player hit gets 1 added to its deaths */
		/* The player who shot the bullet gets 1 added to kills */
	/* If a person is trying to move, allow it unless they are walking into a "solid" object */
		/* A solid object is anything that isn't a space or a bullet */

	return 0;
}

/*
 * This creates a new player with the given information.
 */
Player newPlayer(char av, char *name, char *ip, int port)
{
	Player ret;
	Point initPos;
	int randX, randY;
	if (DEBUG) printf("Starting to populate the Player structure.\n");

	/* Avatar */
	ret.avatar = av;
	/* Name */
	ret.name = name;
	/* IP and port */
	if (DEBUG) printf("Setting the IP as %s\n", ip);
	ret.ip = ip;
	if (DEBUG) printf("Setting the port as %d\n", port);
	ret.port = port;

	/* Score */
	ret.score[0] = 0;
	ret.score[1] = 0;

	/* Randomize the initial position */
	int randFlag = 0;
	while (randFlag == 0)
	{
		randX = (rand() % (MAP_WIDTH - 2)) + 1;// Random number between 1 and 1-board_width
		randY = (rand() % (MAP_HEIGHT - 2)) + 1;
		initPos.x =  randX;
		initPos.y = randY;
		if (theMap[convertPoint(initPos)] == ' ')
		{
			randFlag++;
			break;
		}
	}
	ret.position = initPos;
	return ret;
}

/*
 * This creates a bullet fired in the given direction from the given player
 */
Bullet shotFired(int player, Direction dir, Point playerPos)
{
	Bullet ret;
	Point pos = playerPos;
	ret.dir = dir;
	ret.pID = player;
	switch (dir)
	{
		/* NOTE: Player Position should be (x,y) */
		case NORTH:
			pos.y++;
			break;
		case EAST:
			pos.x++;
			break;
		case SOUTH:
			pos.y--;
			break;
		case WEST:
			pos.x--;
			break;
		default:
			perror("bad enum");
			exit(-1);
			break;
	}
	ret.position = pos;
	return ret;
}
